//
//  CartViewController.m
//  ShopXinyi
//
//  Created by qianfeng on 15/9/14.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "CartViewController.h"
#import "HttpRequestManager.h"
#import "UserInfoModel.h"
#import "CartEmptyCell.h"
#import "CartLikeCell.h"
#import "CartCell.h"
#import "CartLikeGoodCell.h"

@interface CartViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    int _num;
    NSString *_sum;
    NSMutableArray *_dataArray;
}

@property (weak, nonatomic) IBOutlet UIView *sumView;
@property (weak, nonatomic) IBOutlet UILabel *sumLabel;

@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@end

@implementation CartViewController
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _dataArray = [NSMutableArray new];
    }
    return self;
}
- (IBAction)goToAccount:(id)sender {
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [_myTableView registerNib:[UINib nibWithNibName:@"CartEmptyCell" bundle:nil] forCellReuseIdentifier:@"CartEmptyCell"];
    [_myTableView registerNib:[UINib nibWithNibName:@"CartLikeCell" bundle:nil] forCellReuseIdentifier:@"CartLikeCell"];
    [_myTableView registerNib:[UINib nibWithNibName:@"CartCell" bundle:nil] forCellReuseIdentifier:@"CartCell"];
    [_myTableView registerNib:[UINib nibWithNibName:@"CartLikeGoodCell" bundle:nil] forCellReuseIdentifier:@"CartLikeGoodCell"];
    
    [self requestDataFromNet];
    
}


-(void)requestDataFromNet
{
    UserInfoModel *userInfo = [UserInfoModel shareInstance];
    [[HttpRequestManager shareInstance] postCartInfoWithKey:userInfo.key success:^(id responseObj) {
        NSLog(@"购物车responseObj==%@",responseObj);
        NSDictionary *dic = responseObj;
        NSDictionary *datasDic = dic[@"datas"];
        _num = [datasDic[@"num"] intValue];
        _sum = datasDic[@"sum"];
        if (_num > 0) {
            _sumView.hidden = NO;
        }else{
            _sumView.hidden = YES;
        }
        
        [_myTableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -UITableViewDelegate&UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_num > 0) {
        return (int)_num;
    }else
        return 2;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_num > 0) {
        CartCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CartCell"];
        return cell;
    }else{
        if (indexPath.row == 0) {
            CartEmptyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CartEmptyCell"];
            return cell;
        }else{
            CartLikeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CartLikeCell"];
            return cell;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_num > 0) {
        return 100;
    }else
        return _myTableView.bounds.size.width/1.6;
}



@end
